package com.xdap.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdap.common.utils.GsonUtil;
import com.xdap.dao.*;
import com.xdap.pojo.jimu.*;
import com.xdap.service.JmReportExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class JmReportExportServiceImpl implements JmReportExportService {

    @Autowired
    private DictDao dictDao;
    @Autowired
    private DictItemDao dictItemDao;
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private ReportDataSourceDao reportDataSourceDao;
    @Autowired
    private ReportDbDao reportDbDao;
    @Autowired
    private ReportDbParamDao reportDbParamDao;
    @Autowired
    private ReportLinkDao reportLinkDao;
    @Autowired
    private ReportDbFieldDao reportDbFieldDao;
    @Autowired
    private ReportShareDao reportShareDao;

    @Override
    public void export(HttpServletResponse httpServletResponse,String name) throws Exception {
        // 导出数据
        // jimu_report_data_source 数据库
        List<JimuReportDataSourcePo> jimuReportDataSourceList = reportDataSourceDao.list();
        // 获取非默认数据库的信息
        List<String> noLocalHostIds =
                jimuReportDataSourceList.stream().filter(sourcePo -> !(sourcePo.getDbUrl().contains("127.0.0.1")))
                        .map(JimuReportDataSourcePo::getId)
                        .collect(Collectors.toList());

        // jimu_report_db 数据报表 -> 报表设计--会存jimu_report_data_source的id
        List<JimuReportDbPo> jimuReportDbList = reportDbDao.listBySourceIds(noLocalHostIds);

        // 从JimuReportDbPo 里面获取所有的reportId，然后再导出jimu_report
        // jimu_report 实际报表
        List<String> reportIds =
                jimuReportDbList.stream().map(JimuReportDbPo::getJimuReportId).collect(Collectors.toList());
        List<JimuReportPo> reportPoList = reportDao.list(reportIds);

        // jimu_report_db_field sql查询出来实际的字段名称
        List<String> reportDbIds =
                jimuReportDbList.stream().map(JimuReportDbPo::getId).collect(Collectors.toList());
        List<JimuReportDbFieldPo> jimuReportDbFieldPoList = reportDbFieldDao.findByReportDbIds(reportDbIds);

        // jimu_report_db_param sql查询的时候，使用到的参数
        List<JimuReportDbParamPo> jimuReportDbParamPoList = reportDbParamDao.findByReportDbIds(reportDbIds);


        // 导出的信息
        String jimuReportDataSourceStr = GsonUtil.toJson(jimuReportDataSourceList);
        String jimuReportDbStr = GsonUtil.toJson(jimuReportDbList);
        String jimuReportStr = GsonUtil.toJson(reportPoList);
        String jimuReportDbFieldStr = GsonUtil.toJson(jimuReportDbFieldPoList);
        String jimuReportDbParamStr = GsonUtil.toJson(jimuReportDbParamPoList);


        if (Objects.equals(name, "jimuReportDbParam")) {
            download(httpServletResponse, jimuReportDbParamStr, name);
        } else if (Objects.equals(name, "jimuReport")) {
            download(httpServletResponse, jimuReportStr, name);
        } else if (Objects.equals(name, "jimuReportDb")) {
            download(httpServletResponse, jimuReportDbStr, name);
        } else if (Objects.equals(name, "jimuReportDbField")) {
            download(httpServletResponse, jimuReportDbFieldStr, name);
        }else if (Objects.equals(name, "jimuReportDataSource")) {
            download(httpServletResponse, jimuReportDataSourceStr, name);
        }
    }

    public String download(HttpServletResponse response, String json, String name) throws Exception {
        // 设置内容类型为JSON
        response.setContentType("application/json");
        // 设置文件名
        String fileName = name + ".json";
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

        // 序列化为JSON字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = objectMapper.writeValueAsString(json);

        try (OutputStream outputStream = response.getOutputStream()) {
            outputStream.write(jsonString.getBytes(StandardCharsets.UTF_8));
            outputStream.flush();
        }
        return "下载成功";
        }


}
